package com.bdqn;

import com.bdqn.pojo.SysUser;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;

@Controller
@RequestMapping("/user")
@Scope("prototype")
public class SysUserController {
    private Logger logger = Logger.getLogger(SysUserController.class);
    private ArrayList<SysUser> userList = new ArrayList<SysUser>();
    // 用于返回查询到的用户数据
    private ArrayList<SysUser> queryUserList = new ArrayList<SysUser>();
    public SysUserController() {
        try {
            userList.add(new SysUser(100, "haoyuliu", "豪宇刘","123456",1,new Date(),"110120119","地球市中国区",1,1,new Date(),1,new Date(), 23,"1"));
            userList.add(new SysUser(100, "haoyuliu", "豪宇刘","123456",1,new Date(),"110120119","地球市中国区",1,1,new Date(),1,new Date(), 23,"1"));
            userList.add(new SysUser(100, "haoyuliu", "豪宇刘","123456",1,new Date(),"110120119","地球市中国区",1,1,new Date(),1,new Date(), 23,"1"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping
    public String list(Model model) {
        logger.info("当查询条件为空时，查询用户信息" + this);
        queryUserList.clear();
        queryUserList.addAll(userList);
        model.addAttribute("queryUserList", userList);
        return "sysUser/sysUserList";
    }

    @PostMapping("list")
    public String list(@RequestParam(value = "realName", required = false) String realName, Model model) {
        logger.info("查询条件：【realName】=" + realName + "，查询用户信息");
        queryUserList.clear();
        if (realName != null && ! realName.equals("")) {
            for (SysUser user : userList) {
                if (user.getRealName().indexOf(realName) != -1) {
                    queryUserList.add(user);
                }
            }
        } else {
            queryUserList.addAll(userList);
        }
        model.addAttribute("queryUserList", queryUserList);
        return "sysUser/sysUserList";
    }

}
